package com.ljt.framework.test;

import static org.mockito.Matchers.any;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.ljt.framework.test.demo.UserAction;
import com.ljt.framework.test.demo.UserService;

/**
 * @Project : framework-test
 * @Program Name : com.ljt.framework.test.MockForPrivateDemo.java
 * @Description : 对私有方法测试
 * @Author : wangchao
 * @Creation Date : 2016年4月13日 下午8:51:17
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest({ UserService.class, UserAction.class })
public class MockForPrivateDemo {

	@Test
	public void demo() throws Exception {
		UserService userService = PowerMockito.spy(new UserService());

		// 模拟返回值私有方法.
		PowerMockito.doReturn("mock").when(userService, "secreteSayHello", any(String.class));
		// 模拟私有空方法.
		PowerMockito.doNothing().when(userService, "secreteSayHi", any(String.class));

		// 设置业务服务.
		UserAction userAction = new UserAction();
		userAction.setUserService(userService);

		// 调用业务方法.
		userAction.executeForPrivate("private");

		// 验证.
		PowerMockito.verifyPrivate(userService, Mockito.times(1)).invoke("secreteSayHello", any(String.class));
		PowerMockito.verifyPrivate(userService, Mockito.times(1)).invoke("secreteSayHi", any(String.class));
	}

}
